$(document).ready(function() {
    var now=new Date();
    var day = ("0" + now.getDate()).slice(-2);
    var month = ("0" + (now.getMonth() + 1)).slice(-2);
    var MyDate = now.getFullYear()+"-"+(month)+"-"+(day);
    $("#tdate").val(MyDate);
    $("#b_add").click(function() {
        var content=$("#tquestion").val();
        if(content==""){
            return;
        }
        $.ajax({
            type:"POST",
            url:"/question/addContent",
            data:{
                content:content
            },
            dataType:"json",
            success:function (data) {
                if(data==null){
                    alert("添加失败！");
                }
                else{
                    $("#tquestion").val("");
                    var table=$("#table_question");
                    table.append("<tr><td>"+data.id+"</td><td>"+data.content+"</td><td><input type='button' class='b_delete' value='删除' /></td></tr>");
                    $(".b_delete").click(function() {
                        $(this).parent().parent().remove();
                    })
                }
            },
            error:function (err) {
                console.log(err);
            }
        })
    })
    $("#b_model").click(function() {
        $.ajax({
            type:"get",
            url:"/question/getAllQuestion",
            dataType:"json",
            success:function (data) {
                var t1=$("#t1");
                var table=$("#table_question");
                for(var i in data) {
                    t1.append("<tr><td>" + data[i].id + "</td><td>" + data[i].content + "</td><td><input type='button' class='b_add' value='选择' /></td></tr>");
                }
                $('#t1').DataTable( {
                    retrieve: true
                });
                $(".b_add").click(function () {
                    var id=$(this).parent().parent().find("td").eq(0).text();
                    var content=$(this).parent().parent().find("td").eq(1).text();
                    table.append("<tr><td>"+id+"</td><td>"+content+"</td><td><input type='button' class='b_delete' value='删除' /></td></tr>");
                    $(".b_delete").click(function() {
                        $(this).parent().parent().remove();
                    })
                })
            }
        })
    })

    $("#b_submit").click(function() {
        var date1=$("#tdate").val();
        var clazzId=$("#p_clazzId").html();
        var selfExam={
            date:date1,
            clazzId:clazzId
        }
        $.ajax({
            type:"GET",
            url:"/selfExam/checkByDateAndClazzId",
            data:selfExam,
            dateType:"json",
            success:function (data) {
                if(data==null||data==""){
                    insertSelfExam();
                }
                else{
                    alert("该日期已发布过结对作业！");
                }
            },
            error:function (err) {
                console.log(err);
            }
        })
    })
    function insertSelfExam() {
        var date=$("#tdate").val();
        var questionId="";
        var clazzId=$("#p_clazzId").html();
        var length=$("#table_question").find("tr").length ;
        for(var i=1;i<length;i++) {
            questionId+=$("#table_question").find("tr").eq(i).find("td").eq(0).text();
            questionId+=",";
        }
        $.ajax({
            type:"post",
            url:"selfExam/insertSelfExam",
            data:{
                date:date,
                questionId:questionId,
                clazzId:clazzId
            },
            dataType:"text",
            success:function (data) {
                if(data==1){
                    alert("发布成功！");
                    window.location.href = "/teamExam";
                }
                else{
                    alert("发布失败！");
                }
            },
            error:function (err) {
                console.log(err);
            }
        })
    }

    var str="";

    function initDate(){
        var now=new Date();
        now.setDate(now.getDate()+1);
        var day = ("0" + now.getDate()).slice(-2);
        var month = ("0" + (now.getMonth() + 1)).slice(-2);
        var MyDate = now.getFullYear()+"-"+(month)+"-"+(day) ;
        $(document.getElementById("tdate")).val(MyDate);
    }

    function checkDate(){
        var tdate=document.getElementById("tdate").value;
        ajaxCall("Post",path+"/SelfServlet?method=checkDate&date="+tdate,checkResult);
    }
    function checkResult(res) {
        var obj=JSON.parse(res);
        if(obj){
            setQuestion();
        }
        else{
            alert("该日期已发布过竞赛作业！请更换日期");
        }
    }

    function setQuestion(){
        var tdate=document.getElementById("tdate").value;
        var tableId = document.getElementById("tques");

        var classId=document.getElementById("s1").value;
        //var rows = tab.rows.length ;
        var trs = tableId.rows;
        if(trs.length==1){
            alert("提交内容为空!!");
            return;
        }
        for(var i = 1, len = trs.length ; i < len; i++) {
            var id = trs[i].cells[0].innerHTML;
            str += id + ",";

        }
        ajaxCall("POST",path+"/SelfServlet?method=setQuestion&tquestionId="+str+"&tdate="+tdate+"&classId="+classId,getResult);

    }


    function getResult(res) {
        var obj = JSON.parse(res);
        if(obj){
            alert("添加成功！");
            location.reload();
        }else{
            alert("添加失败！");
        }
    }

})


